<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    button{
      position: fixed;
      right: 50px;
      bottom: 50px;
    }
  </style>
</head>
<body style="height: 3000px">
  <div id="app">
    <button v-backtop="200">回到顶部</button>
  </div>
 <script src="./vue.js"></script>
 <script>
    Vue.directive('backtop', {
      inserted(el, binding){
        // 初始化隐藏
        el.style.display = 'none';
        window.onscroll = function(){
          var sTop = document.documentElement.scrollTop || document.body.scrollTop;
          if(sTop > binding.value){
            el.style.display = 'block';
          }else{
            el.style.display = 'none';
          }
        }
        el.onclick = function(){
          window.scrollTo(0,0);
        }
      },
      unbind(){
        window.scroll = null;
      }
    })
    const vm = new Vue({
      el: '#app'
    })
 </script>
</body>
</html>